﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Affirma.ThreeSharp.Wrapper;
using System.Xml;
using System.Collections;
using log4net;

namespace CloudDrive.S3
{
    /// <summary>
    /// Caches files locally
    /// </summary>
    public class FileCache : IDictionary<string, FileObject>
    {
        private Dictionary<string, FileObject> _Files;
        private ILog _Logger;
        private IThreeSharpWrapper _ThreeSharpWrapper;
        private string _BucketName;
        private object _locker;
        public FileCache(IThreeSharpWrapper threeSharpWrapper, string bucketName)
        {
            _BucketName = bucketName;
            _ThreeSharpWrapper = threeSharpWrapper;
            _Logger = log4net.LogManager.GetLogger(typeof(FileCache));
        }
        private Dictionary<string, FileObject> Files
        {
            get
            {
                lock (_locker)
                {
                    if (_Files == null)
                    {
                        try
                        {
                            string xmlResponse = _ThreeSharpWrapper.ListBucket(_BucketName);
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.LoadXml(xmlResponse);
                            XmlNodeList nav = xmlDoc.SelectNodes("/ListBucketResult/Contents/Key");
                            _Files = new Dictionary<string, FileObject>(StringComparer.CurrentCultureIgnoreCase);
                            foreach (XmlNode node in nav)
                                _Files.Add(node.InnerText, new FileObject());
                        }
                        catch (Exception ex)
                        {
                            _Files = null;
                            _Logger.Warn("Error populating file cache", ex);
                        }
                    }
                }
                return _Files;
            }
        }
        #region IDictionary<string,FileObject> Members

        public void Add(string key, FileObject value)
        {
            throw new NotImplementedException();
        }

        public bool ContainsKey(string key)
        {
            throw new NotImplementedException();
        }

        public ICollection<string> Keys
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(string key)
        {
            throw new NotImplementedException();
        }

        public bool TryGetValue(string key, out FileObject value)
        {
            throw new NotImplementedException();
        }

        public ICollection<FileObject> Values
        {
            get { throw new NotImplementedException(); }
        }

        public FileObject this[string key]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICollection<KeyValuePair<string,FileObject>> Members

        public void Add(KeyValuePair<string, FileObject> item)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(KeyValuePair<string, FileObject> item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(KeyValuePair<string, FileObject>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(KeyValuePair<string, FileObject> item)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,FileObject>> Members

        public IEnumerator<KeyValuePair<string, FileObject>> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
